package edu.ua.Team_Industry;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class AreaOverlay extends Overlay {

ArrayList<GeoPoint> geoPoints;
public Industry_Map indMap;
public int color;
public AreaTag areaTag;

public AreaOverlay(ArrayList<GeoPoint> points, int color)
{
    geoPoints = points;
    this.color = color;
}

public AreaOverlay()
{
	geoPoints = new ArrayList<GeoPoint>();
}

public void setMap(Industry_Map iMap) {
	this.indMap = iMap;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
	if (!geoPoints.isEmpty()) {
    //Set the color and style
    Paint paint = new Paint();
    if (color == 1) {
    	paint.setColor(Color.argb(80, 255, 0, 0));
    }
    if (color == 2) {
    	paint.setColor(Color.argb(80, 0, 0, 255));
    }
    if (color == 3) {
    	paint.setColor(Color.argb(80, 0, 255, 0));
    }
    if (color == 4) {
    	paint.setColor(Color.argb(80, 255, 255, 0));
    }
    if (color == 5) {
    	paint.setColor(Color.argb(80, 238, 130, 238));
    }
    	paint.setStyle(Paint.Style.FILL);

    //Create path and add points
    Path path = new Path();
    Point firstPoint = new Point();
    mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
    path.moveTo(firstPoint.x, firstPoint.y);

    for(int i = 1; i < geoPoints.size(); ++i)
    {
        Point nextPoint = new Point();
        mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
        path.lineTo(nextPoint.x, nextPoint.y);
    }

    //Close polygon
    path.lineTo(firstPoint.x, firstPoint.y);
    path.setLastPoint(firstPoint.x, firstPoint.y);
    canvas.drawPath(path, paint);
    super.draw(canvas, mapView, shadow);
	}

}

public GeoPoint getCenter(){
	int lats = 0;
	int lngs = 0;
	for(int i = 0; i < geoPoints.size(); ++i)
    {
        lats += geoPoints.get(i).getLatitudeE6();
        lngs += geoPoints.get(i).getLongitudeE6();
    }
	int avgLat = lats / geoPoints.size();
	int avgLng = lngs / geoPoints.size();
	return new GeoPoint(avgLat, avgLng);
}
}